﻿var readList = new Array();
var areaList =new Array();
var areaLen=0;
var readLen = 0;
var parentDiv=null;
var mapHeight = 768;
var mapWidth = 1024;

function getId(id) {
	return "string" == typeof id ? document.getElementById(id) : id;
}


function buildMap(imgName, imgAddr, width, height, parentID) {
	parentDiv = getId(parentID);
	var currentDiv = document.createElement("DIV");
	currentDiv.setAttribute("id", "mapDiv")
	currentDiv.style.background = "url(monitorAction!getMap.action) no-repeat";
	currentDiv.style.textAlign = "center";
	currentDiv.style.position = "absolute";
	currentDiv.style.zIndex = "9";
	currentDiv.style.height = height + "px";
	currentDiv.style.width = width + "px";
	currentDiv.style.title = imgName;
	currentDiv.style.border = "1px solid #eaeaea";
	currentDiv.style.cursor = "pointer";
	currentDiv.style.margin = "0px";
	currentDiv.style.padding = "0px";
	currentDiv.style.top = "0px";
	currentDiv.style.left = "0px";
	currentDiv.oncontextmenu=new Function("return false;");
	parentDiv.appendChild(currentDiv);
};

function _buildAllMap()
{
	parent_id="mainDiv";
	element_id = "mapDiv";
	parent_id="mainDiv";
	
	var currentDiv = document.createElement("DIV");
	
	currentDiv.style.textAlign = "center";
	currentDiv.style.position = "absolute";
	currentDiv.style.zIndex = "9";
//	currentDiv.style.height = height + "px";
//	currentDiv.style.width = width + "px";
	currentDiv.style.title = "全部地图";
	currentDiv.style.border = "1px solid #eaeaea";
	currentDiv.style.cursor = "pointer";
	currentDiv.style.margin = "0px";
	currentDiv.style.padding = "0px";
	currentDiv.style.top = "0px";
	currentDiv.style.left = "0px";
	currentDiv.oncontextmenu = new Function("return false;");
	
	img1 = _createMapImg("1");
	
	img2 = _createMapImg("2");
	
	img3 = _createMapImg("3");
	
	currentDiv.appendChild(img1);
	
	currentDiv.appendChild(img2);
	
	currentDiv.appendChild(img3);
	
	parentDiv = getId(parent_id);
	
	parentDiv.appendChild(currentDiv);
}

function _createMapImg(img_seq)
{
	default_map_width = mapWidth;
	default_map_height = mapHeight;
	
	img_url = "images/" + img_seq + ".svg";
	
	var mapImg = document.createElement("img");
	mapImg.setAttribute("id", "mapImg");
	mapImg.setAttribute("src", img_url);
	//The following two magic lines must be put here!
	//If you comment it out, you will find a very annoying bug:
	//the second createElement call to create a svg img object will return with stale state and 
	//in this state you will not be able to change image scale for that img object;
	mapImg.style.width = default_map_width*2 + "px";
	mapImg.style.height = default_map_height*2 + "px";
	
	return mapImg;
}

function _buildMap(imgName, image_seq)
{
	parent_id="mainDiv";
	element_id = "mapDiv";
	
	//if an element with the same id exists, delete it first!
	var old_element = document.getElementById(element_id);
	if(old_element)
	{
		p = old_element.parentNode;
		p.removeChild(old_element);
	}
	
	parentDiv = getId(parent_id);
	
	var currentDiv = document.createElement("DIV");
	currentDiv.setAttribute("id", "mapDiv");

	var mapImg = _createMapImg(image_seq);
	
	currentDiv.appendChild(mapImg);
	currentDiv.style.textAlign = "center";
	currentDiv.style.position = "absolute";
	currentDiv.style.zIndex = "9";
//	currentDiv.style.height = height + "px";
//	currentDiv.style.width = width + "px";
	currentDiv.style.title = imgName;
	currentDiv.style.border = "1px solid #eaeaea";
	currentDiv.style.cursor = "pointer";
	currentDiv.style.margin = "0px";
	currentDiv.style.padding = "0px";
	currentDiv.style.top = "0px";
	currentDiv.style.left = "0px";
	document.body.style.overflow = "auto";
	document.getElementById("mainDiv").style.overflow="none";
	currentDiv.oncontextmenu = new Function("return false;");
	parentDiv.appendChild(currentDiv);
}

function buildRead(lrmID, lrmName, lrmType, lrmX, lrmY, lrmState, lrmTime,
		lrmRemark) {
	readList[readLen] = new Array();
	readList[readLen][0] = lrmID;
	readList[readLen][1] = lrmName;
	readList[readLen][2] = lrmType;
	readList[readLen][3] = parseInt(lrmX)*2;
	readList[readLen][4] = parseInt(lrmY)*2;
	readList[readLen][5] = lrmState;
	readList[readLen][6] = lrmTime;
	readList[readLen][7] = lrmRemark;
	readLen = readLen + 1;
	drawRead(lrmID, lrmName, parseInt(lrmX)*2, parseInt(lrmY)*2, lrmState);
};
function buildArea(areaID,areaName, areaType,areaX, areaY,readers) {
	areaList[areaLen] = new Array();
	areaList[areaLen][0] = areaID;
	areaList[areaLen][1] = areaName;
	areaList[areaLen][2] = areaType;
	areaList[areaLen][3] = areaX;
	areaList[areaLen][4] = areaY;
	areaList[areaLen][5] = readers;
	areaLen = areaLen + 1;
	drawArea(areaID,areaName, areaType, areaX, areaY);
};
function commitRead(lrmID, lrmX, lrmY) {
	$.post("/SCMDB/readerAction!resetRead.action", {
		"lrmID" : lrmID,
		"lrmX" : lrmX,
		"lrmY" : lrmY
	}, function(data) {
		
	});
}
function commitArea(areaID, areaX, areaY) {
	$.post("/SCMDB/areaAction!resetArea.action", {
		"id" : areaID,
		"areaX" : areaX,
		"areaY" : areaY
	}, function(data) {
		
	});
}
function drawArea(areaID,areaName, areaType, areaX, areaY) {
	var parentDiv=getId("mapDiv");
	var currentDiv = document.createElement("DIV");
	currentDiv.setAttribute("id", "area" + areaID);
	currentDiv.setAttribute("title","区域名称："+areaName+" 类型："+areaType);
	currentDiv.style.background = "url(/SCMDB/images/area.gif) no-repeat";
	currentDiv.style.position = "absolute";
	currentDiv.style.zIndex = "200";
	currentDiv.style.border = "0px solid #000";
	currentDiv.style.cursor = "pointer";
	currentDiv.style.margin = "0px";
	currentDiv.style.height = "16px";
	currentDiv.style.width = "16px";
	currentDiv.style.padding = "0px";
	currentDiv.style.left = areaX + "px";
	currentDiv.style.top = areaY + "px";
	currentDiv.oncontextmenu=new Function("return false;");
	parentDiv.appendChild(currentDiv);
	moveFlag="area";
}
function drawRead(lrmID, lrmName, lrmX, lrmY, lrmState) {
	var parentDiv=getId("mapDiv");
	var currentDiv = document.createElement("DIV");
	currentDiv.setAttribute("id", "read" + lrmID);
	currentDiv.setAttribute("title", "读头号："+lrmID+",读卡器名称："+lrmName);
	currentDiv.style.background = "url(/SCMDB/images/reader-1.gif) no-repeat";
	currentDiv.style.position = "absolute";
	currentDiv.style.border = "0px solid #000";
	currentDiv.style.cursor = "pointer";
	currentDiv.style.margin = "0px";
	currentDiv.style.height = "16px";
	currentDiv.style.width = "5px";
	currentDiv.style.padding = "0px";
	currentDiv.style.left = lrmX + "px";
	currentDiv.style.top = lrmY + "px";
	currentDiv.style.zIndex = "200";
	currentDiv.style.paddingLeft="16px";
	currentDiv.style.color="red";
	//currentDiv.innerText=lrmID+","+lrmName;
	currentDiv.oncontextmenu=new Function("return false;");
	parentDiv.appendChild(currentDiv);
	moveFlag="read";
}
